import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

public class DoodleCanvasListener 
	implements MouseListener, MouseMotionListener{
	
	protected PasswordGen password;
	private int xp0,yp0,xp1,yp1;
		
	private Vector<LastSquare> vectorLastSquare;
	protected Vector<Vector<LastSquare>> vectors;
	private RunScript rs = new RunScript();
	
	public DoodleCanvasListener(DoodleCanvas canvas, PasswordGen password){
		this.canvas = canvas;
		this.password=password;
		this.vectorLastSquare = canvas.vectorLastSquare;
		this.vectors = canvas.vectors;

	}
	
	protected DoodleCanvas canvas;
	
	public void mousePressed(MouseEvent e){
		Point p = e.getPoint();
		
		
		
		canvas.mouseButtonDown = true;
		canvas.x = p.x;
		canvas.y = p.y;
		//System.out.println("0: "+p.x+ "  "+ p.y);
		password.setVars(canvas.width, canvas.height);
		xp0=p.x;
		yp0=p.y;
		xp1=xp0;
		yp1=yp0;
		password.setPassord(xp0,yp0);
		//canvas.glowCell(p.x, p.y);
	}
	public void mouseDragged(MouseEvent e){
		Point p = e.getPoint();
		LastSquare last = new LastSquare();
		
		xp0=p.x;
		yp0=p.y;
		if (canvas.mouseButtonDown){
			//canvas.glowCell(p.x, p.y);
			canvas.getOffScreenGraphics().drawLine(canvas.x, canvas.y, p.x, p.y);
			int x0 = Math.min(canvas.x, p.x);
			int y0 = Math.min(canvas.y, p.y);
			int dx = Math.abs(p.x-canvas.x)+1;
			int dy = Math.abs(p.y-canvas.y)+1;
			canvas.repaint(x0, y0, dx, dy);
			last.p1x=canvas.x;
			last.p1y=canvas.y;
			last.p2x=p.x;
			last.p2y=p.y;
			canvas.x = p.x;
			canvas.y = p.y;
			password.setPassord(xp0,yp0,xp1, yp1);
			
			if(password.hasChangedSquare){
				
				drawLastSquare();
			}
			
			vectorLastSquare.add(last);
			
			
			xp1=p.x;
			yp1=p.y;
			
		}
	}
	
	public void mouseReleased(MouseEvent e){
		canvas.mouseButtonDown = false;
		
//		int x=;
//		int y=;		
		password.setPassord( (canvas.width + 1),(canvas.height + 1));
	}
	
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseMoved(MouseEvent e){
//		Point p = e.getPoint();
//		if (canvas.mouseButtonDown){
//			canvas.glowCell(p.x, p.y);
//		}
	}
	
	
	public void drawLastSquare(){
		LastSquare last = new LastSquare();
		int x0=0,y0=0;
		x0=vectorLastSquare.firstElement().p1x;
		y0=vectorLastSquare.firstElement().p1y;
		//finds the square
		x0=(x0*canvas.n_Columns)/canvas.width;
		y0=(y0*canvas.n_Rows)/canvas.height;
		
		
		
		Vector<LastSquare> v = new Vector<LastSquare>();
		int index=(canvas.n_Columns * y0) + x0;
		v = vectors.get(index);	//get vector of LastSquare from vectors
		v.addAll(vectorLastSquare);	//add new "draw"  to v
		vectors.add(index, v);	//add all back to vectors, to that position
		vectors.remove(index+1);	//removes old, that was shifted when added
		
//		for(int i=0;i<vectorLastSquare.size();i++){
//			last=vectorLastSquare.elementAt(i);
//			canvas.getOffScreenGraphics().drawLine(last.p1x,last.p1y,last.p2x,last.p2y);
//
//		}
		v=vectors.get(index);
		for(int i=0;i<v.size();i++){
			last=v.get(i);
			canvas.getOffScreenGraphics().drawLine(last.p1x,last.p1y,last.p2x,last.p2y);

		}
		
		canvas.repaint(x0*(canvas.getCol_Step()), y0*(canvas.getRow_Step()), canvas.getCol_Step(), canvas.getRow_Step());
		vectorLastSquare.removeAllElements();
		
	}
	
	public void setRunScript(RunScript rs){
		this.rs=rs;
	}
	
	
}

//holds points to draw lines on last square (after erasing the glowing square)
class LastSquare{
	public int p1x, p1y;
	public int p2x, p2y;
}